//
//  ZSTheWavesVIew.m
//  MyDemo2
//
//  Created by 张帅 on 16/11/23.
//  Copyright © 2016年 张帅. All rights reserved.
//

#import "ZSTheWavesVIew.h"

@interface ZSTheWavesVIew ()
@property (nonatomic,strong) CADisplayLink * display;    //刷屏器
@property (nonatomic,strong) CAShapeLayer * outsideWavesLayer; //外面浪layer
@property (nonatomic,strong) CAShapeLayer * insideWavesLayer;//里面浪layer
@property (nonatomic, assign) CGFloat offset; //用来计算速度
@property (nonatomic,strong) UIImageView * imageView;
@end

@implementation ZSTheWavesVIew

- (id)initWithFrame:(CGRect)frame {
    self=[super initWithFrame:frame];
    if (self) {
        [self initData];
    }
    return self;
}
-(void)initData {
    self.wavesHeight=5;
    self.bending=2;
    self.speed=1;
    [self addSubview:self.imageView];
//    self.outsideWavesLayerColor=[UIColor redColor];
    self.insideWavesLayerColor=[[UIColor whiteColor] colorWithAlphaComponent:0.4];
    [self.layer addSublayer:self.insideWavesLayer];
     [self.layer addSublayer:self.outsideWavesLayer];
}
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView=[[UIImageView alloc] initWithFrame:self.bounds];
    }
    return _imageView;
}
//外层浪
- (CAShapeLayer *)outsideWavesLayer {
    if (!_outsideWavesLayer) {
        _outsideWavesLayer=[CAShapeLayer layer];
        CGRect frame=[self bounds];
        //把layer设置到底部
        frame.origin.y=self.frame.size.height-self.wavesHeight;
        frame.size.height=self.wavesHeight;
        _outsideWavesLayer.frame=frame;
        ///设置layer颜色
        _outsideWavesLayer.fillColor=self.outsideWavesLayerColor.CGColor;
    }
    return  _outsideWavesLayer;
}
//里面浪
- (CAShapeLayer *)insideWavesLayer {
    if (!_insideWavesLayer) {
        _insideWavesLayer=[CAShapeLayer layer];
        CGRect frame =[self bounds];
        frame.origin.y=self.frame.size.height-self.wavesHeight;
        frame.size.height=self.wavesHeight;
        _insideWavesLayer.frame=frame;
        _insideWavesLayer.fillColor=self.insideWavesLayerColor.CGColor;
    }
    return _insideWavesLayer;
}
- (void)tartAnimation {
    self.display=[CADisplayLink displayLinkWithTarget:self selector:@selector(drawAnimation)];
    [self.display addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
//绘制动画
- (void)drawAnimation {
    self.offset+=self.speed;
    CGFloat width =CGRectGetWidth(self.frame);
    
    ///创建外层浪path、
    CGMutablePathRef outsidePath = CGPathCreateMutable();
    //外面浪path第一个点
    CGPathMoveToPoint(outsidePath, NULL, 0, self.wavesHeight);
    CGFloat y=0.0;
    ///创建里面浪path、
    CGMutablePathRef insidePath = CGPathCreateMutable();
    //里面浪path第一个点
    CGPathMoveToPoint(insidePath, NULL, 0, self.wavesHeight);
    CGFloat insideY =0.0;
    
    for (CGFloat x=0.0; x<=width; x++) {
        /**
         这个循环会 从左至右依次绘制触波浪的形状
         sinf（CGFloat）； 在这里面计算的数怎么加减乘除都不会超过1
         */
        y=self.wavesHeight*sinf(0.01 * self.bending *x+self.offset*0.045);
        //里面浪y开始点事个外面浪相反的所以是-y；
        insideY=-y;
        //开始绘制外面浪
        CGPathAddLineToPoint(outsidePath, NULL, x, y);
        //开始绘制里面浪
        CGPathAddLineToPoint(insidePath, NULL, x, insideY);
    }
    //回调波浪波动y值
    CGFloat headY =CGRectGetWidth(self.frame) /2;
    headY=self.wavesHeight*sinf(0.01*self.bending*headY+self.offset*0.045);
    if (self.blockY) {
        self.blockY(headY);
    }
    
    CGPathAddLineToPoint(insidePath, NULL, width, self.wavesHeight);
    CGPathAddLineToPoint(insidePath, NULL, 0, self.wavesHeight);
    ///关闭path
    CGPathCloseSubpath(insidePath);
    _insideWavesLayer.path=insidePath;
    //填充不波浪颜色
    _insideWavesLayer.fillColor=self.insideWavesLayerColor.CGColor;
    ///释放path
    CGPathRelease(insidePath);
    
    //外面浪
    CGPathAddLineToPoint(outsidePath, NULL, self.frame.size.width, self.wavesHeight);
    CGPathAddLineToPoint(outsidePath, NULL, 0, self.wavesHeight);
    ///关闭path
    CGPathCloseSubpath(outsidePath);
    _outsideWavesLayer.path=outsidePath;
    //填充不波浪颜色
    _outsideWavesLayer.fillColor=self.outsideWavesLayerColor.CGColor;
    ///释放path
    CGPathRelease(outsidePath);
}
///停止动画
- (void)stupAnimatiom {
    [self.display invalidate];
    self.display = nil;
}
- (void)setPicUrl:(NSString *)picUrl {
    _picUrl=picUrl;
    _imageView.image=[UIImage imageNamed:_picUrl];
}


@end
